<template>
  <div class="container">
    <div class="export-header">
      <el-button type="primary" size='medium' @click="$router.push('/roomEdit')">新增办公室</el-button>
      <el-button
        size='medium'
        type="primary"
        @click="$router.back()"
        style="float: right;"
      >返回</el-button>
    </div>
    <div class="input_modal">
      <div class="input-item">
        <span>办公室名称：</span>
        <el-input v-model="name" placeholder="请输入内容" class="proName" @keyup.enter.native="search"></el-input>
      </div>
      <div class="input-item">
        <span>状态：</span>
        <el-select v-model="isSale" placeholder="请选择">
          <el-option
            label="上架"
            value="1">
          </el-option>
          <el-option
            label="下架"
            value="0">
          </el-option>
        </el-select>
      </div>
      <div class="input-item">
        <span>是否是首页推荐：</span>
        <el-select v-model="isRecommend" placeholder="请选择">
          <el-option
            label="是"
            value="1">
          </el-option>
          <el-option
            label="否"
            value="0">
          </el-option>
        </el-select>
      </div>
      <div class="input-item">
        <el-button icon="el-icon-search" type="primary" size="medium" @click="getList">查询</el-button>
      </div>
    </div>
    <div class="table_con">
      <el-table v-loading="loading" :data="tableData" :border='true'>
        <el-table-column :formatter="formatter" prop="name" label="办公室名称"></el-table-column>
        <el-table-column :formatter="formatter" prop="unitPrice" label="单价"></el-table-column>
        <el-table-column :formatter="formatter" prop="totalPrice" label="总价"></el-table-column>
        <el-table-column :formatter="formatter" prop="buildingName" label="所属写字楼"></el-table-column>
        <el-table-column :formatter="formatter" prop="isSale" label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.isSale == 0">下架</div>
            <div v-else-if="scope.row.isSale == 1">上架</div>
          </template>
        </el-table-column>
        <el-table-column :formatter="formatter" prop="isRecommend" label="是否是首页推荐">
          <template slot-scope="scope">
            <div v-if="scope.row.isRecommend == 0">否</div>
            <div v-else-if="scope.row.isRecommend == 1">是</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleRecommend(scope.row)">{{scope.row.isRecommend == 0 ? '设为推荐' : '取消推荐'}}</el-button>
            <el-button type="primary" size="mini" @click="$router.push('/roomDetail?id='+scope.row.id+'&pageIndex='+pageIndex)">查看</el-button>
            <el-button type="primary" size="mini" @click="handleUpDown(scope.row)" :style="scope.row.isSale != 0 ? 'background: #fd2024!important;border: none;' : ''">{{scope.row.isSale == 0 ? '上架' : '下架'}}</el-button>
            <el-button type="primary" size="mini" @click="$router.push('/roomEdit?id='+scope.row.id+'&pageIndex='+pageIndex)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageIndex" :page-size="pageSize" :page-sizes='[10,20,30,40]' layout="total, sizes, prev, pager, next, jumper" :total="pageCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  let CONSTANT = require("../../constant/constant.js");
  let common = require("../../common.js");
  export default {
    data() {
      return {
        tableData: [],
        pageCount: null,
        pageIndex: 1,
        pageSize: 10,
        loading: true,
        mobile: null,
        nickname:null,
        name: "",
        isSale: "",
        isRecommend: "",
      };
    },
    mounted() {
      this.pageIndex = this.$router.currentRoute.query.pageIndex||1;
      this.getList();
    },
    methods: {
      formatter(row, column, cellValue) {
        return cellValue ? cellValue : "- -";
      },
      getList: function() {
        let url = CONSTANT.URL.ROOM.list;
        let data = {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          name: this.name,
          isSale: this.isSale,
          isRecommend: this.isRecommend,
        };
        this.loading = true;
        common.requestAjax(url, JSON.stringify(data), null, res => {
          this.loading = false;
          let data = res.data;
          this.tableData = data.bussData;
          this.pageCount = data.count;
        });
      },
      search: function(e) {
        if (e.keyCode == 13) {
          this.pageIndex = 1;
          this.getList();
        }
      },
      handleRecommend(item) {
        let url = CONSTANT.URL.ROOM.recommend;
        common.put(url, item.id+"/recommend/"+(item.isRecommend == 0 ? 'y' : 'n'), '', res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: item.isRecommend == 0 ? "设为推荐成功" : '取消推荐成功'
            });
            this.getList();
          } else {
            this.$message({
              type: "warning",
              message: res.data.errMsg
            });
          }
        });
      },
      handleUpDown(item) {
        let url = CONSTANT.URL.ROOM.shelf;
        common.put(url, item.id+"/shelf/"+(item.isSale == 0 ? 'y' : 'n'), '', res => {
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: item.isSale == 0 ? "上架成功" : '下架成功'
            });
            this.getList();
          } else {
            this.$message({
              type: "warning",
              message: res.data.errMsg
            });
          }
        });
      },
      handleDel(id) {
        this.$confirm("确定删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let url = CONSTANT.URL.ROOM.del;
            common.del(url, id, res => {
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.getList();
              } else {
                this.$message({
                  type: "warning",
                  message: res.data.errMsg
                });
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.pageIndex = val;
        this.getList();
      }
    }
  };
</script>
